package com.nuoniu.sibanyun.service.cms.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.cms.CmsInfo;
import com.nuoniu.sibanyun.entity.cms.CmsTemplate;
import com.nuoniu.sibanyun.mapper.cms.CmsTemplateMapper;
import com.nuoniu.sibanyun.service.cms.ICmsInfoService;
import com.nuoniu.sibanyun.service.cms.ICmsTemplateService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网站模版管理 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-08-10
 */
@Service
public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplate> implements ICmsTemplateService {

    @Autowired
    private ICmsInfoService cmsInfoService;

    @Override
    public Page<CmsTemplate> pageList(Page page, CmsTemplate template) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        return page(page, wrapper);
    }

    @Override
    public void useTemplate(Integer id) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.last("LIMIT 1");
        CmsInfo cmsInfo = cmsInfoService.getOne(wrapper);

        if(cmsInfo == null) {
            cmsInfo = new CmsInfo();
        }

        cmsInfo.setTemplateId(id);
        cmsInfoService.saveOrUpdate(cmsInfo);
    }
}
